﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.Try"/>.
    /// </summary>
    public class TryVM : BlockStatementVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(Try),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new TryVM((Try)codeObject, isDescription, dictionary); });
        }

        #endregion

        #region /* FIELDS */

        protected ChildListVM<CatchVM> _catchVMs;
        protected FinallyVM _finallyVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.Try"/>.
        /// </summary>
        public TryVM(Try @try, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(@try, isDescription, dictionary)
        {
            _catchVMs = CreateListVM<Catch, CatchVM>(@try.Catches, dictionary);
            _finallyVM = (FinallyVM)CreateVM(@try.Finally, isDescription, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.Try"/> model.
        /// </summary>
        public Try Try
        {
            get { return (Try)CodeObject; }
        }

        /// <summary>
        /// A collection of <see cref="CatchVM"/>s.
        /// </summary>
        public ChildListVM<CatchVM> CatchVMs
        {
            get { return _catchVMs; }
        }

        /// <summary>
        /// True if there are any <see cref="CatchVM"/>s.
        /// </summary>
        public bool HasCatchVMs
        {
            get { return (_catchVMs != null && _catchVMs.Count > 0); }
        }

        /// <summary>
        /// An optional <see cref="FinallyVM"/>.
        /// </summary>
        public FinallyVM FinallyVM
        {
            get { return _finallyVM; }
        }

        /// <summary>
        /// True if there is a <see cref="FinallyVM"/>.
        /// </summary>
        public bool HasFinallyVM
        {
            get { return (_finallyVM != null); }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public override Brush BorderBrush
        {
            get { return Brushes.Pink; }
        }

        public override Brush BackgroundBrush
        {
            get { return Brushes.MistyRose; }
        }

        protected override void RenderAfter(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderAfter(renderer, flags);
            if (HasCatchVMs && !flags.HasFlag(RenderFlags.Description))
            {
                foreach (CatchVM catchVM in _catchVMs)
                    catchVM.Render(renderer, flags | RenderFlags.PrefixSpace);
            }
            if (HasFinallyVM && !flags.HasFlag(RenderFlags.Description))
                _finallyVM.Render(renderer, flags | RenderFlags.PrefixSpace);
        }

        public override void RenderVisible(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderVisible(renderer, flags);
            renderer.RenderVisibleList(_catchVMs, flags | RenderFlags.PrefixSpace);
            if (HasFinallyVM)
                _finallyVM.RenderVisible(renderer, flags | RenderFlags.PrefixSpace);
        }

        public override void UnRender()
        {
            ChildListHelpers.UnRender(_catchVMs);
            if (_finallyVM != null)
                _finallyVM.UnRender();
            base.UnRender();
        }

        #endregion
    }
}
